package main

import (
	"fmt"
	"time"
)

type ListNode struct {
	Val  int
	Next *ListNode
}

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func removeElements(head *ListNode, val int) *ListNode {

	//去掉头部需要删除的内容,保证第一个不是val
	for nil != head && head.Val == val {
		head = head.Next
	}
	if nil == head {
		return head
	}

	p := head
	// t:= head
	for nil != p.Next {
		// fmt.Println(t.Val,val)
		for p.Next.Val == val {
			if p.Next.Next == nil {
				p.Next = nil
				break
			}
			p.Next = p.Next.Next
		}
		p = p.Next
		if p == nil {
			break
		}
	}
	return head

}

func main() {
	start := time.Now()
	node := ListNode{1, nil}
	node1 := ListNode{2, nil}
	node2 := ListNode{3, nil}
	link1 := &node
	node.Next = &node1
	node1.Next = &node2

	val := 2
	dec := removeElements(link1, val)
	cost := time.Since(start)
	for nil != dec {
		fmt.Println(dec.Val)
		dec = dec.Next
	}
	fmt.Println(cost)
}
